package com.wf.ew.sell.biz.impl;

import com.wf.ew.common.BaseDao;
import com.wf.ew.common.PageResult;
import com.wf.ew.common.utils.DateUtil;
import com.wf.ew.common.utils.Utils;
import com.wf.ew.system.model.User;
import com.wf.ew.common.constant.UserConstant;
import com.wf.ew.sell.biz.OutBillBiz;
import com.wf.ew.sell.model.OutBill;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2019-12-06 17:46:49
 * @version ：1.0
 */
@Repository
public class OutBillBizImpl extends BaseDao implements OutBillBiz {

	@Override
	public String list(PageResult<T> page, OutBill outBill, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<OutBill> rm = new BeanPropertyRowMapper<OutBill>(OutBill.class);
		StringBuffer sql = new StringBuffer("select o.id,o.companyId,o.code");
        sql.append(",o.supplier,o.outTime,o.warehouseName,o.remark");
		sql.append(",o.createTime,o.createUser,o.updateTime,o.updateUser");
        sql.append(",cmy.name companyName,suser.name createName ");
        sql.append(" from we_out_bill o ");
        sql.append(" inner join sys_user suser on suser.id=o.createUser ");
        sql.append(" inner join we_company cmy on cmy.id=o.companyId ");
        sql.append(" where o.del=0 and cmy.del=0 ");
		if(!loginUser.getId().equals(UserConstant.ADMIN_ID)){
            sql.append(" and o.companyId=:tablecompanyId ");
            map.put("tablecompanyId", loginUser.getCompanyId());
        }
    	if(Utils.isNotEmpty(outBill)){
			if(Utils.isNotEmpty(outBill.getKeywords())){
				sql.append(" and (instr(o.code,:code)>0 or instr(o.supplier,:code)>0)");
				map.put("code", outBill.getKeywords());
			}
			if(Utils.isNotEmpty(outBill.getDate1())){
				outBill.setDate1(outBill.getDate1()+" 00:00:00");
				sql.append(" and o.outTime>=DATE_FORMAT(:date1,'%Y-%m-%d %H:%i:%s') ");
				map.put("date1", outBill.getDate1());
			}
			if(Utils.isNotEmpty(outBill.getDate2())){
				outBill.setDate2(outBill.getDate2()+" 23:59:59");
				sql.append(" and o.outTime<=DATE_FORMAT(:date2,'%Y-%m-%d %H:%i:%s') ");
				map.put("date2", outBill.getDate2());
			}
			if(Utils.isNotEmpty(outBill.getSort())&&Utils.isNotEmpty(outBill.getOrder())){
				sql.append(" order by "+outBill.getSort()+" "+outBill.getOrder());
			} else {
				sql.append(" order by o.outTime desc ");
			}
		}
		return jsonPage(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<OutBill> list(OutBill outBill, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<OutBill> rm = new BeanPropertyRowMapper<OutBill>(OutBill.class);
        StringBuffer sql = new StringBuffer("select o.id,o.companyId,o.code");
        sql.append(",o.supplier,o.outTime,o.warehouseName,o.remark");
		sql.append(",o.createTime,o.createUser,o.updateTime,o.updateUser");
        sql.append(",cmy.name companyName,suser.name createName ");
        sql.append(" from we_out_bill o ");
        sql.append(" inner join sys_user suser on suser.id=o.createUser ");
        sql.append(" inner join we_company cmy on cmy.id=o.companyId ");
        sql.append(" where o.del=0 and cmy.del=0 ");
		if(!loginUser.getId().equals(UserConstant.ADMIN_ID)){
            sql.append(" and o.companyId=:tablecompanyId ");
            map.put("tablecompanyId", loginUser.getCompanyId());
        }
    	if(Utils.isNotEmpty(outBill)){
			if(Utils.isNotEmpty(outBill.getKeywords())){
				sql.append(" and (instr(o.code,:code)>0 or instr(o.supplier,:code)>0)");
				map.put("code", outBill.getKeywords());
			}
			if(Utils.isNotEmpty(outBill.getDate1())){
				outBill.setDate1(outBill.getDate1()+" 00:00:00");
				sql.append(" and o.outTime>=DATE_FORMAT(:date1,'%Y-%m-%d %H:%i:%s') ");
				map.put("date1", outBill.getDate1());
			}
			if(Utils.isNotEmpty(outBill.getDate2())){
				outBill.setDate2(outBill.getDate2()+" 23:59:59");
				sql.append(" and o.outTime<=DATE_FORMAT(:date2,'%Y-%m-%d %H:%i:%s') ");
				map.put("date2", outBill.getDate2());
			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public OutBill getById(OutBill outBill) {
		RowMapper<OutBill> rm = new BeanPropertyRowMapper<OutBill>(OutBill.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select o.id,o.companyId,o.code");
        sql.append(",o.supplier,o.outTime,o.warehouseName,o.remark");
		sql.append(",o.createTime,o.createUser,o.updateTime,o.updateUser");
        sql.append(",cmy.name companyName,suser.name createName ");
        sql.append(" from we_out_bill o ");
        sql.append(" inner join sys_user suser on suser.id=o.createUser ");
        sql.append(" inner join we_company cmy on cmy.id=o.companyId ");
        sql.append(" where o.del=0 and cmy.del=0 and o.id=:id ");
		map.put("id", outBill.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public KeyHolder add(OutBill outBill, User loginUser) {
		int count = 1;
		KeyHolder keyHolder=new GeneratedKeyHolder();
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into we_out_bill (");
        sql.append("companyId,code,supplier,outTime");
		sql.append(",warehouseName,remark,createUser) values ( ");
        sql.append(":companyId,:code,:supplier,:outTime");
		sql.append(",:warehouseName,:remark,:createUser)");
        map.put("companyId", loginUser.getCompanyId());
        map.put("code", outBill.getCode());
        map.put("supplier", outBill.getSupplier());
        map.put("outTime", outBill.getOutTime());
        map.put("warehouseName", outBill.getWarehouseName());
        map.put("remark", outBill.getRemark());
		map.put("createUser", loginUser.getId());
		getMapParameter(sql, map);
		outBill.setCompanyId(loginUser.getCompanyId());
		outBill.setCreateUser(Integer.valueOf(loginUser.getId()));
		SqlParameterSource paramSource=new BeanPropertySqlParameterSource(outBill);
		count = jdbcTemplate.update(sql.toString(), paramSource,keyHolder,new String[]{"id"});
		return keyHolder;
	}

	@Override
	public boolean update(OutBill outBill, User loginUser) {
		int count = 0;
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update we_out_bill set ");
        sql.append("supplier=:supplier,outTime=:outTime ");
		sql.append(",warehouseName=:warehouseName,remark=:remark,updateTime=now() ");
		sql.append(",updateUser=:updateUser where id=:id ");
        map.put("supplier", outBill.getSupplier());
        map.put("outTime", outBill.getOutTime());
        map.put("warehouseName", outBill.getWarehouseName());
        map.put("remark", outBill.getRemark());
		map.put("updateUser", loginUser.getId());
		map.put("id", outBill.getId());
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean delete(OutBill outBill, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(outBill.getIds().length()>0){
			for(String ID : outBill.getIds().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update we_out_bill set del=1" +
						",updateUser=:updateUser, updateTime=now() " 
						+ " where id =:id ");
				map.put("updateUser", loginUser.getId());
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
